package gomoku;

public class GameLogic {

	//private int gridMatrix[][] = new int[15][15];
	private Board board = new Board();
	public boolean playerOnesTurn = true;
	private boolean gameOver = false;
	//Create AI
	private AlphaBetaAgent gLADOS = new AlphaBetaAgent();
		
	public GameLogic(){
		/*
		//INITIALIZE THE MATRIX
		for(int i=0; i<15;i++){
			for(int j=0; j<15;j++){
				gridMatrix[i][j] = 0;
			}
		}*/
	}
	
	public void restartGame() {
		board.reset();
		/*
		for(int i=0; i<15;i++){
			for(int j=0; j<15;j++){
				gridMatrix[i][j] = 0;
			}
		}
		*/
		playerOnesTurn = true;
		gameOver = false;
		
	}
	
	private void callOnAi(){
				
			Move response;
			do{
			response = gLADOS.alphaBetaSearch(board);
			}while(!update(response.getX(), response.getY()));
					
	}
	
	public boolean update(int i, int j){	
		//Probably better to make this return a boolean, true if the stone was
		//placed at i,j successfully. And have a surrounding function to handle gameover,
		//occupied space and ai moves.
		if(!gameOver && board.getPosValue(i, j) == 0){
			ply(i,j);

			//Let's see if that was a winning move...
			int player = 1;
			if(!playerOnesTurn) player = 2;
			if(terminalTest(player)) {
				System.out.println("And the winner is, Player " + player);
				gameOver = true;
			}
			//Otherwise, other player's turn.
			playerOnesTurn = !playerOnesTurn;
			
			//////////////////////////////////////////////
			//Here would be a good place to call the AI //
			if(!playerOnesTurn && !gameOver){
				callOnAi();
			}
			
			return true;
			
		}
		else if(gameOver){
			System.out.println("GAME OVER MAN! GAME OVER!");
			return false;
		}
		else{
			System.out.println("That intersection is already claimed! Choose another.");
			return false;
		}
	}
	
	public int getPosInfo(int i, int j){
		return board.getPosValue(i,j);
	}
	private void ply(int i, int j){
		if(playerOnesTurn){
			board.putStone(i, j, 1); //[i][j] = 1;
		}
		else{
			board.putStone(i, j, 2); //gridMatrix[i][j] = 2;
		}
	}
	
	private boolean terminalTest(int player){
		
		for(int a = 0;a<15;a++){
			for(int b = 0;b<11;b++){
				if(checkFiveToTheRight(a, b, player)) return true;
			}
		}
		for(int a = 0;a<15;a++){
			for(int b = 0;b<11;b++){
				if(checkFiveDown(b, a, player)) return true;
			}
		}
		for(int a = 0;a<11;a++){
			for(int b = 0;b<11;b++){
				if(checkFiveDiagonalDownRight(a, b, player)) return true;
			}
		}	
		for(int a = 4;a<15;a++){
			for(int b = 0;b<11;b++){
				if(checkFiveDiagonalUpRight(a, b, player)) return true;
			}
		}
		return false;
	}
	
	private boolean checkFiveToTheRight(int i, int j, int player){
		
		for(int k = 0;k<5;k++){
			if(player != board.getPosValue(i, k+j)) return false;
		}
		return true;
	}
	private boolean checkFiveDown(int i, int j, int player){
		
		for(int k = 0;k<5;k++){
			if(player != board.getPosValue(i+k, j)) return false;
		}
		return true;
	}
	private boolean checkFiveDiagonalDownRight(int i, int j, int player){
		
		for(int k = 0;k<5;k++){
			if(player != board.getPosValue(i+k, k+j)) return false;
		}
		return true;
	}
	private boolean checkFiveDiagonalUpRight(int i, int j, int player){
		
		for(int k = 0;k<5;k++){
			if(player != board.getPosValue(i-k, j+k)) return false;
		}
		return true;
	}
	
	
	
}
